# encoding: utf-8
'''
Created: 2014-04-28 12:48

@author: VAN
'''


from HTMLParser import HTMLParser
import json
import os
import urllib2
import zlib


def checkfile(filename):
    return os.path.exists(filename)

def similerjsonstr2dict(strval):
    return eval(strval.replace('{', 'dict(').replace('}', ')').replace(':', '='))


def jsonp(html):
    index = html.find('{')
    rindex = html.rfind(')')
    return json.loads(html[index:rindex]);

def req_handler(url, header):
    req = urllib2.Request(url=url, headers=header)
    rsp = urllib2.urlopen(req)
    rsp_headers = rsp.info()
    rsp_html = rsp.read()
    if 'Content-Encoding' in rsp_headers and rsp_headers['Content-Encoding'] == 'gzip':
        rsp_html = zlib.decompress(rsp_html, 16 + zlib.MAX_WBITS)
    
    return rsp_html

def htmlunescape(text):
    html_parser = HTMLParser();
    return html_parser.unescape(text)
   


